package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.SysRoleApplicationMapper;
import com.zjdiepu.www.ibiz.base.ISysRoleApplicationService;
import com.zjdiepu.www.model.base.dto.SysRoleApplication;
import com.zjdiepu.www.model.base.ext.SysRoleApplicationExt;

/**
 * 角色菜单对应表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月05日 16时02分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysRoleApplicationServiceImpl extends BaseService implements ISysRoleApplicationService {
	public static final Logger logger = LoggerFactory.getLogger(SysRoleApplicationServiceImpl.class);

	@Autowired
	private SysRoleApplicationMapper sysRoleApplicationMapper;
	
	@Override
	public void saveUpdateAndDelete(SysRoleApplication sysRoleApplication, OperateType operType) {
		switch (operType) {
		case add:
			sysRoleApplication.initInsertData(sysRoleApplication.getModifier(), sysRoleApplication.getModifierName());
			sysRoleApplicationMapper.insert(sysRoleApplication);
			break;
		case edit:
			sysRoleApplication.initUpdateData(sysRoleApplication.getModifier(), sysRoleApplication.getModifierName());
			// 影响的记录数
			int updateCount = sysRoleApplicationMapper.updateByPrimaryKeySelective(sysRoleApplication);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysRoleApplication.initDeleteData(sysRoleApplication.getModifier(), sysRoleApplication.getModifierName());
			// 影响的记录数
			int deleteCount = sysRoleApplicationMapper.updateByPrimaryKeySelective(sysRoleApplication);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysRoleApplication> selectByExt(SysRoleApplicationExt sysRoleApplicationExt) {
		return sysRoleApplicationMapper.selectByExt(sysRoleApplicationExt);
	}
	
	@Override
	public int countByExt(SysRoleApplicationExt sysRoleApplicationExt){
		return sysRoleApplicationMapper.countByExt(sysRoleApplicationExt);
	}
	
	@Override
	public SysRoleApplication selectById(String id) {
		return sysRoleApplicationMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysRoleApplicationMapper.clearEmpty(fieldNames, id);
		}
	}

	@Override
	public void save(List<SysRoleApplication> sysRoleApplications) {
		if(CollectionUtils.isNotEmpty(sysRoleApplications)){
			sysRoleApplicationMapper.insertBatch(sysRoleApplications);
		}
	}

	@Override
	public void delete(SysRoleApplication sysRoleApplication) {
		sysRoleApplicationMapper.delete(sysRoleApplication);
	}
}
